/**
 * 
 */
package com.smarthome.web.property.controller.system;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.ads.AdsService;
import com.smarthome.domain.bean.ShAds;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.web.property.utils.UserHolder;

/**
 * @author dhc
 * @date 2015年11月23日 下午10:14:00
 * @title
 */
@Controller
@RequestMapping(value="/ads")
public class AdsController {

	@Resource(name="adsService")
	private AdsService adsService;
	
	
	@RequestMapping("/dataGrid")
	@ResponseBody
	public DataGrid dataGrid(ShAds ads,PageHelper page){
		ads.setResidenceId(UserHolder.getCurrentResidenceId());
		return adsService.queryDataGrid(ads, page);
	}
	
	/**
     * 跳转到广告添加页面
     * 
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(HttpServletRequest request) {
        return "/system/ads/adsAdd";
    }
    /**
     * 跳转到广告添加页面
     * 
     * @return
     */
    @RequestMapping("/eidtPage")
    public String editPage(HttpServletRequest request,Long id) {
    	request.setAttribute("ads", adsService.ShAdsById(id));
    	return "/system/ads/adsEdit";
    }
    
    /**
     * 添加广告
     * 
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result add(ShAds ads) {
    	if(StringUtils.isBlank(ads.getUrl())){
    		return Result.failure("地址不能为空");
    	}
    	if(StringUtils.isBlank(ads.getTitle())){
    		return Result.failure("标题不能为空");
    	}
    	ads.setResidenceId(UserHolder.getCurrentResidenceId());
    	adsService.saveAds(ads);    	
        return Result.success("添加成功！");
    }
    /**
     *  编辑广告
     * 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Result eidt(ShAds ads) {
    	if(null == ads.getId()){
    		return Result.failure("地址不能为空");
    	}
    	if(StringUtils.isBlank(ads.getUrl())){
    		return Result.failure("地址不能为空");
    	}
    	if(StringUtils.isBlank(ads.getTitle())){
    		return Result.failure("标题不能为空");
    	}
    	
    	ads.setResidenceId(UserHolder.getCurrentResidenceId());
    	adsService.updateAds(ads);    	
    	return Result.success("添加成功！");
    }
    
    
    /**
     *  编辑广告
     * 
     * @return
     */
    @RequestMapping("/open")
    @ResponseBody
    public Result open(Long id) {
    	ShAds ads = adsService.ShAdsById(id);
    	ads.setStatus(1);
    	adsService.updateAds(ads);    	
    	return Result.success("开启广告成功成功！");
    }
    /**
     *  编辑广告
     * 
     * @return
     */
    @RequestMapping("/close")
    @ResponseBody
    public Result close(Long id) {
    	ShAds ads = adsService.ShAdsById(id);
    	ads.setStatus(0);
    	adsService.updateAds(ads);    	
    	return Result.success("关闭广告成功成功！");
    }
    
    
}
